package com.unisoc.keyguard;

import com.android.keyguard.EmergencyButton;
import com.android.keyguard.KeyguardMessageArea;
import com.android.keyguard.KeyguardSecurityView;
import com.android.keyguard.KeyguardUpdateMonitorCallback;
import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.KeyguardSecurityCallback;
import com.android.systemui.Dependency;
import com.android.systemui.R;
import com.unisoc.keyguard.util.SimLockUtil;
import com.android.internal.widget.LockPatternUtils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.ColorStateList;
import android.telephony.TelephonyManager;
import android.widget.LinearLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * Displays a PIN pad for unlocking operator simlock
 */
public class KeyguardOperatorSimLockView extends LinearLayout implements KeyguardSecurityView {
    private static final String TAG = "KeyguardOperatorSimLockView";

    private TextView mSimlockMsgView;
    private TextView mDismissButton;
    private EmergencyButton mEmergencyButton;
    private KeyguardMessageArea mSecurityMessageDisplay;

    private boolean mDismissProhibited = false;
    private boolean mShowing = false;

    private KeyguardSecurityCallback mCallback;
    private LockPatternUtils mLockPatternUtils;
    private boolean mEnableHaptics;
    private boolean mResumed;

    KeyguardUpdateMonitorCallback mUpdateMonitorCallback = new KeyguardUpdateMonitorCallback() {

        @Override
        public void onSimStateChanged(int subId, int slotId, int simState) {
            Log.d(TAG, "onSimStateChanged(subId=" + subId + ", slotId=" + slotId + ", state=" + simState + ")");
            switch(simState) {
            case TelephonyManager.SIM_STATE_ABSENT:
                if (mShowing && mCallback != null) {
                    mCallback.dismiss(true, KeyguardUpdateMonitor.getCurrentUser());
                }
                break;
            default:
                break;
            }
        }
    };

    public KeyguardOperatorSimLockView(Context context) {
        this(context, null);
    }

    public KeyguardOperatorSimLockView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void reset() {
        Log.d(TAG, "reset()");
        refreshViews(mDismissProhibited);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.d(TAG, "onFinishInflate");

        mDismissProhibited = KeyguardSimLockMonitor.getInstance(mContext).hideBtnForOperator();
        mShowing = true;

        mSimlockMsgView = findViewById(R.id.kg_simlock_view_message);
        mSimlockMsgView.setText(com.android.internal.R.string.operator_simlock_view_title);

        mDismissButton = findViewById(R.id.kg_simlock_dismiss_button);
        mDismissButton.setText(com.android.internal.R.string.operator_simlock_view_btn_dismiss);
        mDismissButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "dismiss SimLock dialog");
                doHapticKeyClick();
                KeyguardSimLockMonitor.getInstance(mContext).setSimLockCanceled(true);
                KeyguardSimLockMonitor.getInstance(getContext()).reportOperatorSimLockCanceled();
                if (mCallback != null) {
                    mCallback.dismiss(true, KeyguardUpdateMonitor.getCurrentUser());
                }
            }
        });
        mEmergencyButton = findViewById(R.id.emergency_call_button);
        mEmergencyButton.setText(com.android.internal.R.string.operator_simlock_view_btn_eca);

        refreshViews(mDismissProhibited);
    }

    // Cause a VIRTUAL_KEY vibration
    public void doHapticKeyClick() {
        if (mEnableHaptics) {
            performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY,
                    HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING
                    | HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
        }
    }

    private void refreshViews(boolean dismissProhibited) {
        Log.d(TAG,"refreshViews: dismissProhibited = " + dismissProhibited);
        if (mSecurityMessageDisplay != null && mResumed) {
            mSecurityMessageDisplay.setMessage("");
        }
        if (mDismissButton != null) {
            mDismissButton.setVisibility(dismissProhibited? View.GONE : View.VISIBLE);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.d(TAG, "onAttachedToWindow");
        mSecurityMessageDisplay = KeyguardMessageArea.findSecurityMessageDisplay(this);
        Log.d(TAG, "onAttachedToWindow: mSecurityMessageDisplay" + mSecurityMessageDisplay);
        Dependency.get(KeyguardUpdateMonitor.class).registerCallback(mUpdateMonitorCallback);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.d(TAG, "onDetachedFromWindow");
        Dependency.get(KeyguardUpdateMonitor.class).removeCallback(mUpdateMonitorCallback);
        mDismissProhibited = false;
        mShowing = false;
    }

    @Override
    public void onPause() {
        Log.d(TAG, "onPause");
        mResumed = false;
    }

    @Override
    public void onResume(int reason) {
        mResumed = true;
    }

    @Override
    public void setKeyguardCallback(KeyguardSecurityCallback callback) {
        mCallback = callback;
    }

    @Override
    public KeyguardSecurityCallback getCallback() {
        return mCallback;
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        reset();
    }

    @Override
    public void showUsabilityHint() {
    }

    @Override
    public void startAppearAnimation() {
    }

    @Override
    public boolean startDisappearAnimation(Runnable finishRunnable) {
        return false;
    }

    @Override
    public void showPromptReason(int reason) {
    }

    @Override
    public void showMessage(CharSequence message, ColorStateList colorState) {
        Log.d(TAG, "showMessage");
        if (mSecurityMessageDisplay != null && mResumed) {
            mSecurityMessageDisplay.setMessage("");
        }
    }

    public boolean disallowInterceptTouch(MotionEvent event) {
        return false;
    }

    @Override
    public boolean needsInput() {
        return false;
    }

    @Override
    public void setLockPatternUtils(LockPatternUtils utils) {
        mLockPatternUtils = utils;
        mEnableHaptics = mLockPatternUtils.isTactileFeedbackEnabled();
    }

    @Override
    public CharSequence getTitle() {
        return getContext().getString(com.android.internal.R.string.operator_simlock_view_title);
    }
}

